package calao.plan;

import calao.storage.DataBinding;
import calao.storage.DataReader;
import calao.storage.DataWriter;
import calao.storage.Field;
import calao.storage.Record;
import calao.storage.RecordBinding;
import calao.storage.Sequence;
import calao.storage.Table;
import calao.plan.client.Resource;
import calao.plan.client.WorkPeriod;

class ResourceDB {

	static final DataBinding<WorkPeriod> WORK_PERIOD_BINDING = 
		new DataBinding<WorkPeriod>(WorkPeriod.class, "plan.WorkPeriod") {
			public WorkPeriod readObject(DataReader reader, int version) {
				return new WorkPeriod(reader.readInt(), reader.readInt(), reader.readInt(),
						reader.readInt(), reader.readInt());
			}
			public void write(DataWriter writer, WorkPeriod object) {
				writer.write(object.getWeekDay());
				writer.write(object.getStartHour());
				writer.write(object.getStartMin());
				writer.write(object.getEndHour());
				writer.write(object.getEndMin());				
			}
	};
	
	static final RecordBinding<Resource> INFO_BINDING =
		new RecordBinding<Resource>(Resource.class) {
			@Override
			public Resource toObject(Record record) {
				Resource info = new Resource(record.getInt(ID));
				info.setWorkPeriods(record.getAll(WORK_PERIODS));
				return info;
			}
			@Override
			public Record toRecord(Resource object) {
				Record record = new Record(ResourceDB.TABLE);
				record.set(ID, object.getId());
				record.set(WORK_PERIODS, object.getWorkPeriods());
				return record;
			}
	};
	
	static final Table TABLE = new Table("calao.plan.Resource"); 

	static final Sequence ID_SEQUENCE = new Sequence("calao.plan.ResourceId");
	
	public static final Field<Integer> ID = 
		TABLE.createField("id", Field.INTEGER);	

	public static final Field<WorkPeriod> WORK_PERIODS = 
		TABLE.createField("workPeriods", WorkPeriod.class, true);	

	static {
		TABLE.createPrimaryKeyIndex(ID);
	}
	
}
